//
//  LocalProductViewController.m
//  SXH
//
//  Created by 房 国生 on 15/6/24.
//  Copyright (c) 2015年 lyss. All rights reserved.
//

#import "LocalProductViewController.h"
#import "LPTableViewCell.h"
#import "CommonService.h"

#import "LPEntity.h"
#import "UIImageView+WebCache.h"
#import "LocalProductDetailViewController.h"
#import "YYAnimationIndicator.h"

#import "AppDelegate.h"

#import "MJRefresh.h"

@interface LocalProductViewController ()
{
    YYAnimationIndicator *indicator;
    MJRefreshHeaderView *_header;
    MJRefreshFooterView *_footer;
    int page;
}
@end

@implementation LocalProductViewController
@synthesize tableViewLP, mutableArrayLP;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"特产";
    mutableArrayLP = [[NSMutableArray alloc] init];
    
    page = 1;
    [self addHeader];
    [self addFooter];
    if (self.isTabVC) {
        self.backBtn.hidden = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [mutableArrayLP count];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *couponTableViewCellIdentifier=@"LPTableViewCell";
    LPTableViewCell *cell = (LPTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"LPTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    LPEntity *entity = [mutableArrayLP objectAtIndex:indexPath.row];
    NSString *url = [NSString stringWithFormat:@"%@/%@", IMAGE_URL, entity.album];
    [cell.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:url]];
    cell.labelName.text= entity.title;
    cell.oldPriceLabel.text = [NSString stringWithFormat:@"原价：%@",entity.old_price];
    cell.actualPriceLabel.text = entity.price;
    cell.numlabel.text = [NSString stringWithFormat:@"月销售%@笔",entity.sale_count];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH*350/750;
//    return 150;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //NewsEntity *entity = [arraySpot objectAtIndex:indexPath.row];
    LPEntity *entity = [mutableArrayLP objectAtIndex:indexPath.row];
    LocalProductDetailViewController *vc = [[LocalProductDetailViewController alloc] init];
    vc.wrapEntity = entity;
    [self.navigationController pushViewController:vc animated:YES];
    
}

- (void) getLPData:(int) pageNo RefreshView:(MJRefreshBaseView *)refreshView{
    NSString *strPageNo = [NSString stringWithFormat:@"%d", pageNo];
    CommonService *service = [[CommonService alloc] init];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                           self.cityid, @"cityid",
//                           self.parkid, @"parkid",
                           strPageNo, @"page",
                           @"20", @"limit", nil];
    
    [service getNetWorkData:param
                       Path:LPPATH
                  Successed:^(id entity) {
                      NSDictionary *dic = (NSDictionary *) entity;
                      NSMutableArray *array = [dic objectForKey:@"rows"];;
                      if (pageNo ==1) {
                          [mutableArrayLP removeAllObjects];
                          page = 1;
                      }
                      for (NSDictionary* attributes in array) {
                          LPEntity *entity = [[LPEntity alloc] init];
                          entity = [entity initListWithAttributes:attributes];
                          [mutableArrayLP addObject:entity];
                      }
                      [self doneWithView:refreshView];
                  } Failed:^(NSInteger errorCode, NSString *message) {
                      [self doneWithView:refreshView];
                      DebugLog(@"%@", message);
                  }];
}


- (void)addFooter
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshFooterView *footer = [MJRefreshFooterView footer];
    footer.scrollView = self.tableViewLP;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        
        ++page;
        [self getLPData:page RefreshView:refreshView];
        
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    _footer = footer;
}

- (void)addHeader
{
    //__unsafe_unretained OAAnnoViewController *vc = self;
    MJRefreshHeaderView *header = [MJRefreshHeaderView header];
    header.scrollView = self.tableViewLP;
    
    header.beginRefreshingBlock = ^(MJRefreshBaseView *refreshView) {
        // 进入刷新状态就会回调这个Block
        
        [self getLPData:1 RefreshView:refreshView];
        NSLog(@"%@----开始进入刷新状态", refreshView.class);
    };
    header.endStateChangeBlock = ^(MJRefreshBaseView *refreshView) {
        // 刷新完毕就会回调这个Block
        NSLog(@"%@----刷新完毕", refreshView.class);
    };
    header.refreshStateChangeBlock = ^(MJRefreshBaseView *refreshView, MJRefreshState state) {
        // 控件的刷新状态切换了就会调用这个block
        switch (state) {
            case MJRefreshStateNormal:
                NSLog(@"%@----切换到：普通状态", refreshView.class);
                break;
                
            case MJRefreshStatePulling:
                NSLog(@"%@----切换到：松开即可刷新的状态", refreshView.class);
                break;
                
            case MJRefreshStateRefreshing:
                NSLog(@"%@----切换到：正在刷新状态", refreshView.class);
                break;
            default:
                break;
        }
    };
    [header beginRefreshing];
    _header = header;
}

- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    // 刷新表格
    [self.tableViewLP reloadData];
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [refreshView endRefreshing];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"LocalProductViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"LocalProductViewController"];
}
@end


